package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;


//添加业务层的注解，交给spring管理
@Service
//添加事务控制的注解
@Transactional
public class CourierServiceImpl implements CourierService {
    
	//注入持久层对象
	@Autowired
	private CourierDao courierDao;
	
	
	
	@Override
	public void save(Courier courier) {
		courierDao.save(courier);

	}



	@Override
	public Page<Courier> findPageData(Specification<Courier> specification, Pageable pageable) {
		Page<Courier> all = courierDao.findAll(specification, pageable);
		return all;
	}



	@Override
	public void delBatch(String[] arr) {
	//在这里做数组的遍历
	for(String id :arr ){
		
		courierDao.updateDeltag(Integer.parseInt(id));	
		
	}	
		
		
	}



	@Override
	public List<Courier> findnoassociation() {
	Specification<Courier> specification=new Specification<Courier>() {

		@Override
		public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		
		//查询条件，判断快递员关联的定区列表为空
			
			Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
			
			return p;
		}
	};
	//调用 dao层处理业务
	
		return courierDao.findAll(specification);
	}



	
	}


